summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.java
blob: d5009bc60227f7b05bb9b52a21e7698cbcd5350d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package org.yuzu.yuzu_emu.ui.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.yuzu.yuzu_emu.NativeLibrary;
import org.yuzu.yuzu_emu.R;
import org.yuzu.yuzu_emu.activities.EmulationActivity;
import org.yuzu.yuzu_emu.features.settings.ui.SettingsActivity;
import org.yuzu.yuzu_emu.model.GameProvider;
import org.yuzu.yuzu_emu.ui.platform.PlatformGamesFragment;
import org.yuzu.yuzu_emu.utils.AddDirectoryHelper;
import org.yuzu.yuzu_emu.utils.DirectoryInitialization;
import org.yuzu.yuzu_emu.utils.FileBrowserHelper;
import org.yuzu.yuzu_emu.utils.FileUtil;
import org.yuzu.yuzu_emu.utils.GpuDriverHelper;
import org.yuzu.yuzu_emu.utils.PicassoUtils;
import org.yuzu.yuzu_emu.utils.StartupHandler;
import org.yuzu.yuzu_emu.utils.ThemeUtil;

/**
 * The main Activity of the Lollipop style UI. Manages several PlatformGamesFragments, which
 * individually display a grid of available games for each Fragment, in a tabbed layout.
 */
public final class MainActivity extends AppCompatActivity implements MainView {
    private Toolbar mToolbar;
    private int mFrameLayoutId;
    private PlatformGamesFragment mPlatformGamesFragment;

    private MainPresenter mPresenter = new MainPresenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ThemeUtil.applyTheme();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();

        setSupportActionBar(mToolbar);

        mFrameLayoutId = R.id.games_platform_frame;
        mPresenter.onCreate();

        if (savedInstanceState == null) {
            StartupHandler.handleInit(this);
            mPlatformGamesFragment = new PlatformGamesFragment();
            getSupportFragmentManager().beginTransaction().add(mFrameLayoutId, mPlatformGamesFragment).commit();
        } else {
            mPlatformGamesFragment = (PlatformGamesFragment) getSupportFragmentManager().getFragment(savedInstanceState, "mPlatformGamesFragment");
        }
        PicassoUtils.init();

        // Dismiss previous notifications (should not happen unless a crash occurred)
        EmulationActivity.tryDismissRunningNotification(this);
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        if (getSupportFragmentManager() == null) {
            return;
        }
        if (outState == null) {
            return;
        }
        getSupportFragmentManager().putFragment(outState, "mPlatformGamesFragment", mPlatformGamesFragment);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.addDirIfNeeded(new AddDirectoryHelper(this));
    }

    // TODO: Replace with a ButterKnife injection.
    private void findViews() {
        mToolbar = findViewById(R.id.toolbar_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_game_grid, menu);

        return true;
    }

    /**
     * MainView
     */

    @Override
    public void setVersionString(String version) {
        mToolbar.setSubtitle(version);
    }

    @Override
    public void refresh() {
        getContentResolver().insert(GameProvider.URI_REFRESH, null);
        refreshFragment();
    }

    @Override
    public void launchSettingsActivity(String menuTag) {
        SettingsActivity.launch(this, menuTag, "");
    }

    @Override
    public void launchFileListActivity(int request) {
        switch (request) {
            case MainPresenter.REQUEST_ADD_DIRECTORY:
                FileBrowserHelper.openDirectoryPicker(this,
                        MainPresenter.REQUEST_ADD_DIRECTORY,
                        R.string.select_game_folder);
                break;
            case MainPresenter.REQUEST_INSTALL_KEYS:
                FileBrowserHelper.openFilePicker(this,
                        MainPresenter.REQUEST_INSTALL_KEYS,
                        R.string.install_keys);
                break;
            case MainPresenter.REQUEST_SELECT_GPU_DRIVER:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);

                // Get the driver name for the dialog message.
                String driverName = GpuDriverHelper.getCustomDriverName();
                if (driverName == null) {
                    driverName = getString(R.string.system_gpu_driver);
                }

                // Set the dialog message and title.
                builder.setTitle(getString(R.string.select_gpu_driver_title));
                builder.setMessage(driverName);

                // Cancel button is a no-op.
                builder.setNegativeButton(android.R.string.cancel, null);

                // Select the default system driver.
                builder.setPositiveButton(R.string.select_gpu_driver_default, (dialogInterface, i) ->
                {
                    GpuDriverHelper.installDefaultDriver(this);
                    Toast.makeText(this, R.string.select_gpu_driver_use_default, Toast.LENGTH_SHORT).show();
                });

                // Use the file picker to install a custom driver.
                builder.setNeutralButton(R.string.select_gpu_driver_install, (dialogInterface, i) -> {
                    FileBrowserHelper.openFilePicker(this,
                            MainPresenter.REQUEST_SELECT_GPU_DRIVER,
                            R.string.select_gpu_driver);
                });

                // Show the dialog.
                AlertDialog alertDialog = builder.create();
                alertDialog.show();

                break;
        }
    }

    /**
     * @param requestCode An int describing whether the Activity that is returning did so successfully.
     * @param resultCode  An int describing what Activity is giving us this callback.
     * @param result      The information the returning Activity is providing us.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
        super.onActivityResult(requestCode, resultCode, result);
        switch (requestCode) {
            case MainPresenter.REQUEST_ADD_DIRECTORY:
                if (resultCode == MainActivity.RESULT_OK) {
                    int takeFlags = (Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    getContentResolver().takePersistableUriPermission(Uri.parse(result.getDataString()), takeFlags);
                    // When a new directory is picked, we currently will reset the existing games
                    // database. This effectively means that only one game directory is supported.
                    // TODO(bunnei): Consider fixing this in the future, or removing code for this.
                    getContentResolver().insert(GameProvider.URI_RESET, null);
                    // Add the new directory
                    mPresenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result));
                }
                break;

            case MainPresenter.REQUEST_INSTALL_KEYS:
                if (resultCode == MainActivity.RESULT_OK) {
                    int takeFlags = (Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    getContentResolver().takePersistableUriPermission(Uri.parse(result.getDataString()), takeFlags);
                    String dstPath = DirectoryInitialization.getUserDirectory() + "/keys/";
                    if (FileUtil.copyUriToInternalStorage(this, result.getData(), dstPath, "prod.keys")) {
                        if (NativeLibrary.ReloadKeys()) {
                            Toast.makeText(this, R.string.install_keys_success, Toast.LENGTH_SHORT).show();
                            refreshFragment();
                        } else {
                            Toast.makeText(this, R.string.install_keys_failure, Toast.LENGTH_LONG).show();
                            launchFileListActivity(MainPresenter.REQUEST_INSTALL_KEYS);
                        }
                    }
                }
                break;

            case MainPresenter.REQUEST_SELECT_GPU_DRIVER:
                if (resultCode == MainActivity.RESULT_OK) {
                    int takeFlags = (Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    getContentResolver().takePersistableUriPermission(Uri.parse(result.getDataString()), takeFlags);
                    GpuDriverHelper.installCustomDriver(this, result.getData());
                    String driverName = GpuDriverHelper.getCustomDriverName();
                    if (driverName != null) {
                        Toast.makeText(this, getString(R.string.select_gpu_driver_install_success) + " " + driverName, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, R.string.select_gpu_driver_error, Toast.LENGTH_LONG).show();
                    }
                }
                break;
        }
    }

    /**
     * Called by the framework whenever any actionbar/toolbar icon is clicked.
     *
     * @param item The icon that was clicked on.
     * @return True if the event was handled, false to bubble it up to the OS.
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return mPresenter.handleOptionSelection(item.getItemId());
    }

    private void refreshFragment() {
        if (mPlatformGamesFragment != null) {
            NativeLibrary.ResetRomMetadata();
            mPlatformGamesFragment.refresh();
        }
    }

    @Override
    protected void onDestroy() {
        EmulationActivity.tryDismissRunningNotification(this);
        super.onDestroy();
    }
}